<?php

        class Login {
            
            private $login;
            private $id;
            private $gameArray;
            private $dbh;
            
            public function __construct(){
                $db = new Db();
                $this->dbh = $db->getDbh();
                $this->gameArray = array();
            }
            
            public function __get($attr){
			return $this->$attr;
		}
            
            public function connect($login1, $login2){
                $sql = "SELECT player_id, player_login
                       FROM player
                       WHERE player_login = :login1";
                $stmt = $this->dbh->prepare($sql);
                $stmt->bindValue(":login1", $login1);
                $stmt->execute();
                $player1 = $stmt->fetch();
                $sql = "SELECT player_id, player_login
                       FROM player
                       WHERE player_login = :login2";
                $stmt = $this->dbh->prepare($sql);
                $stmt->bindValue(":login2", $login2);
                $stmt->execute();
                $player2 = $stmt->fetch();
                if(!$player1){
                        $this->createLog($login1);
                        $this->connect($login1, $login2);
                }
                elseif( !$player2){
                        $this->createLog($login2);
                        $this->connect($login1, $login2);
                }
                else {
                    $this->login1 = $player1["player_login"];
                    $this->id1 = $player1["player_id"];
                    $this->login2 = $player2["player_login"];
                    $this->id2 = $player2["player_id"];
                }
            }
            
            public function createLog($login){
                $sql = "INSERT INTO player (player_login)
                        VALUES(:login)";
                $stmt = $this->dbh->prepare($sql);
                $stmt->bindValue(":login", $login);
                $stmt->execute();
            }

            public function existGame($idPlayer1, $idPlayer2){
                $sql = "SELECT game_id FROM game
                        WHERE game_player1 = :idPlayer1
                        AND game_player2 = :idPlayer2
                        AND game_winner IS NULL";
                $stmt = $this->dbh->prepare($sql);
                $stmt->bindValue(":idPlayer1", $idPlayer1);
                $stmt->bindValue(":idPlayer2", $idPlayer2);
                $stmt->execute();
                $gameExist = $stmt->fetchAll();
                if(!$gameExist){

                    $sql = "INSERT INTO game (game_player1, game_player2, game_state)
                            VALUES( :idPlayer1, :idPlayer2, 0)";
                    $stmt = $this->dbh->prepare($sql);
                    $stmt->bindValue(":idPlayer1", $idPlayer1);
                    $stmt->bindValue(":idPlayer2", $idPlayer2);
                    $stmt->execute();
                    $this->existGame($idPlayer1, $idPlayer2);
                }
                else {
                    foreach($gameExist as $k => $v){
                        $this->gameArray += array($k => $v);
                    }
                    $this->displayGames($this->gameArray[0]['game_id']);
                }
            }

            public function displayGames($idGame){
                $sql = "SELECT G.* FROM game as G
                        WHERE G.game_player1 = :idPlayer1
                        AND G.game_player2 = :idPlayer2
                        AND G.game_id = :idGame
                        AND G.game_winner IS NULL";
                $stmt = $this->dbh->prepare($sql);
                $stmt->bindValue(":idPlayer1", $this->id1);
                $stmt->bindValue(":idPlayer2", $this->id2);
                $stmt->bindValue(":idGame", $idGame);
                $stmt->execute();
                $game = $stmt->fetch();
                $P1 = $this->playerName($this->id1);
                $P2 = $this->playerName($this->id2);

                echo '<label>'.$P1.' vs '.$P2.'</label></br>';
                echo '<a href="ingame.php?idGame='.$game['game_id'].'&idPlayer1='.$game['game_player1'].'&idPlayer2='.$game['game_player2'].'">Lancer la partie</a>';
            }

            public function playerName($id){
                $sql = "SELECT * FROM player
                        WHERE player_id = :id";
                $stmt = $this->dbh->prepare($sql);
                $stmt->bindValue(":id", $id);
                $stmt->execute();
                $player = $stmt->fetch();
                return $player['player_login'];
            }
        }

?>

